<template>
	<view :class="['card-item',radius ? 'radius' : '']">
		<view class="item-head">
			<view class="left-info">
				<view class="img-wrap container">
					<image :src="item.AvatarUrl"
						mode="widthFix" class="avatar"></image>
				</view>
				<view class="info">
					<view class="head-name">{{ item.PublishName }}</view>
					<view class="head-create">{{ item.CreateDateUtc }}</view>
				</view>
			</view>
		</view>
		<view class="content">
			<view class="img-wrap" v-if="item.ImgList.length == 1">
				<view class="img-box">
					<image v-for="(child, idx) in item.ImgList" :key="idx" :src="child" mode="widthFix" class="img"
						@tap.stop @tap="ViewImage(idx, item.ImgList)"></image>
				</view>
			</view>
			<view class="img-list" v-else>
				<view class="img-box" v-for="(child, idx) in item.ImgList" :key="idx">
					<image :src="child"  class="img" @tap.stop @tap="ViewImage(idx, item.ImgList)"></image>
				</view>
			</view>
			<text-more v-show="item.Content.length > 0" :compRef="getCompRef()" :model="item"></text-more>
			<view class="row bottom-btn">
				<view class="btn-item row_align_center"  @tap.stop="handleLike(item)">
					<li :class="['iconfont',item.IsLike?'icon-like':'icon-unlike']"></li>
					<text>{{item.LikeNum}}</text>
				</view>
				<view class="btn-item row_align_center" @tap.stop="handleComment(item)">
					<li class="iconfont icon-comment"></li>
				</view>
			</view>
			<view v-if="item.CommentList.length > 0" class="comment-box" >
				<view v-for="(comItem,index) in item.CommentList" :key="comItem.Id" class="comment-item">
					<text class="name">
						{{comItem.SendName}}
					</text>
					<template v-if="comItem.Type ==1">
						<text class="relpy">回复</text>
						<text class="name">
							{{comItem.ReceiverName}}
						</text>
					</template>
					<text class="" @click="replyComment(comItem,index)">
						：{{comItem.Content}}
					</text>
				</view>
			</view>
		</view>
		<!-- 评论 -->
		<eject-drawer mode="bottom" :visible="commentState" @close="cancleComment()" drawerWidth="100%"
			drawerHeight="50%">
			<my-comment ref="mycomment" :defaultPlaceholder="replyModel.receiverName?('回复'+replyModel.receiverName+'：'):'评论：'" @cancleComment="cancleComment" @commitComment="commitComment"></my-comment>
		</eject-drawer>
		<!-- 删除评论 -->
		<eject-drawer mode="bottom" :visible="deleteReplyStatus" @close="deleteReplyStatus=false" drawerWidth="100%"
			drawerHeight="20%" class="delete-box">
			<delete-comment @cancleDeletReply="cancleDeletReply" @deleteComment="deleteComment"></delete-comment>
		</eject-drawer>
	</view>
</template>

<script>
	import myComment from "../common/my-comment.vue"
	import ejectDrawer from '@/components/common/eject-drawer'
	import textMore from '@/components/common/text-more'
	import deleteComment from '@/components/community/delete-comment'
	export default {
		props: {
			obj: {
				type: Object
			},
			radius: {
				type: Boolean,
				default: false
			}
		},
		components:{
			myComment,
			ejectDrawer,
			textMore,
			deleteComment
		},
		data() {
			return {
				item: this.obj,
				showPop: false,
				commentList: [],
				commentState:false,
				commentCommunityId:0,
				replyModel:{
					receiverId:0,
					receiverName:"",
					type:0,
					communityImg:"",
					communityContent:""
				},
				deleteReplyStatus:false,
				deleteCommentId:0,
				deleteCommentIndex:-1
			};
		},
		methods: {
			getCompRef(){
				let teamp = +new Date();
				return 'contentEl'+teamp;
			},
			// 回复评论
			replyComment(obj,index){
				this.commentCommunityId = obj.CommunityId;
				// 选中自己的留言 弹出删除提示
				if(obj.SendId == this.$store.state.sender.Id){
					this.deleteReplyStatus = true;
					this.deleteCommentId = obj.Id;
					this.deleteCommentIndex = index;
				}else{
					this.replyModel.receiverId = obj.SendId;
					this.replyModel.receiverName = obj.SendName;
					this.replyModel.type = 1;
					this.commentState = true;
					this.replyModel.communityImg = obj.CommunityImg;
					this.replyModel.communityContent = obj.Content;
				}
			},
			// 点击评论
			handleComment(item){
				this.commentState = true;
				this.commentCommunityId = item.Id;
				this.replyModel.receiverId = item.PublishId;
				this.replyModel.receiverName = "";
				this.replyModel.type = 0;
				this.replyModel.communityImg = item.ImgList.length > 0 ? item.ImgList[0] : "";
				this.replyModel.communityContent = item.Content;
			},
			// 点赞
			handleLike(item){
				this.$emit('handleLike',item)
			},
			// 删除评论
			deleteComment(){
				if(!(this.deleteCommentId > 0)){
					this.$Common.tipMsg("缺乏参数")
					return;				
				}
				let model = {
					id:this.deleteCommentId
				};
				this.$Request.post(this.$Config.APIURL + "/api/deleteCommunityComment", model).then(res => {
					if (res?.code == 200) {
						this.item.CommentList.splice(this.deleteCommentIndex,1);
						this.deleteReplyStatus = false;
					} else {
						this.$Common.tipMsg(res?.message);
					}
				});
			},
			// 取消删除评论
			cancleDeletReply(){
				this.deleteReplyStatus = false;
				this.deleteCommentId = 0;
				this.deleteCommentIndex = -1;
			},
			// 取消评论
			cancleComment(){
				this.commentState = false;
				this.commentCommunityId = 0;
				this.replyModel.receiverId = 0;
				this.replyModel.receiverName = "";
				this.replyModel.communityContent = "";
				this.replyModel.communityImg = "";
				this.replyModel.type = 0;
			},
			// 提交评论
			commitComment(e){
				let model = {
					communityId:this.commentCommunityId,
					sendId:this.$store.state.sender.Id,
					sendName:this.$store.state.sender.Name,
					receiverId:this.replyModel.receiverId,
					receiverName:this.replyModel.receiverName,
					type:this.replyModel.type,
					avatarUrl:this.$store.state.sender.Avatar,
					content:e,
					communityImg:this.replyModel.communityImg,
					communityContent:this.replyModel.communityContent
				}
				this.$Request.post(this.$Config.APIURL + "/api/insertCommunityComment", model).then(res => {
					if (res?.code == 200) {
						this.$refs.mycomment.comment = "";
						let id = this.commentCommunityId
						this.cancleComment();
						this.getCommunityComment(id);
					} else {
						this.$Common.tipMsg(res?.message);
					}
				});
			},
			// 获取评论列表
			getCommunityComment(id){
				let model = {
					communityId:id
				}
				this.$Request.post(this.$Config.APIURL + "/api/getCommunityCommentList", model).then(res => {
					if (res?.code == 200) {
						this.item.CommentList = res?.data
					} else {
						this.$Common.tipMsg(res?.message);
					}
				});
			},
			// 大图
			ViewImage(index, arr) {
				uni.previewImage({
					current: index,
					urls: arr
				});
			}
		},
		created() {
			if (this.obj.picture) {
				this.imgList = this.obj.picture.split(",");
			}
		}
	};
</script>

<style lang="less" scoped>
	@import "@/static/css/icon.css";
	.radius {
		border-radius: 16rpx;
	}
	.delete-box{
		/deep/ .tui-drawer-container{
			border-top-right-radius: 20rpx;
			border-top-left-radius: 20rpx;
		}
	}
	.comment-box{
		background-color: #F7F7F7;
		width: 100%;
		padding: 10rpx 16rpx;
		margin: 0 auto;
		.comment-item{
			line-height: 60rpx;
			.name{
				color: #526B94;
			}
			.relpy{
				padding: 0 8rpx;
			}
		}
	}

	.card-item {
		margin: 40rpx 30rpx 0rpx;
		border-bottom: 1px solid #E8E8E8;
		text-align: left;
		// background-color: #ffffff;
		// box-shadow: 0 20rpx 40rpx 0 rgba(0, 0, 0, 0.1);

		.item-head {
			display: flex;
			align-items: center;
			width: 100%;
			justify-content: space-between;

			.left-info {
				display: flex;
				align-items: center;

				.img-wrap {
					width: 80rpx;
					height: 80rpx;
					overflow: hidden;
					border-radius: 50%;

					.avatar {
						width: 100%;
						height: 80rpx;
					}
				}

				.info {
					padding-left: 40rpx;

					.head-name {
						color: #666666;
						font-size: 28rpx;
					}

					.head-create {
						color: #999999;
						font-size: 20rpx;
					}
				}
			}

			.follow-btn {
				width: 140rpx;
				height: 60rpx;
				line-height: 60rpx;
				border: 1rpx solid #CCCCCC;
				border-radius: 40rpx;
				text-align: center;
			}

			.followed {
				color: #fff;
				background: #FFAF36;
				border: 1rpx solid #FFAF36;
				width: 140rpx;
				height: 60rpx;
				border-radius: 30rpx;
				text-align: center;
				line-height: 60rpx;
			}
		}

		.content {
			.content-other{
				width: 100%;
				height: 40px;
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.like{
					display: flex;
					align-items: center;
					.icon-liked{
						color: #FFAF36;
					}
				}
			}
			.text-content {
				line-height: 40rpx;;
				justify-content: space-between;
				font-size: 30rpx;
				text-align: justify;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 3;
				overflow: hidden;
				margin: 20rpx 0;
			}

			.padding-bottom-lg {
				padding-bottom: 40upx;
			}

			
			.img-list {
				display: flex;
				flex-flow: row wrap;

				.img-box {
					flex: 0 0 30%;
					margin: 0 1% 1%;
					display: flex;
					align-items: center;
					justify-content: center;
					overflow: hidden;
					border-radius: 4px;
					background-color: #ececec;
					margin-top: 26rpx;
					.img {
						    width: 110px;
						    height: 110px;
					}
				}
			}

			.img-wrap {
				.img-box {
					width: 100%;
					max-height: 400rpx;
					overflow: hidden;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 30rpx;
					margin-top: 26rpx;
					.img {
						width: 100%;
					}
				}
			}

			.bottom-btn {
				padding: 20upx;
				justify-content: space-around;
				font-size: 28rpx;
				color: #999999;

				.btn-item {
					width: 33%;
					display: flex;
					align-items: center;
					color: #435257;
					&:nth-child(2){
						justify-content: center;
					}
					&:nth-child(3){
						justify-content: flex-end;
					}

					li {
						margin-right: 20rpx;
						font-size: 22px !important;
						font-weight: 600;
					}
					.icon-like{
						color: #FFCB57;
					}
				}
			}
		}
	}
</style>
